//
//  FangChanViewController.m
//  網易
//
//  Created by 赵天宇 on 16/8/22.
//  Copyright © 2016年 赵天宇. All rights reserved.
//

#import "FangChanViewController.h"
#import "NewTableViewCell.h"
#import "Data.h"
#import <MJRefresh.h>
#import <SDCycleScrollView.h>
#import "NextFangChanViewController.h"
#import "ASIHTTPRequest.h"
#import "AppDelegate.h"
#define  TEST_URL  @"http://apis.baidu.com/txapi/tiyu/tiyu"
#import "Reachability.h"

@interface FangChanViewController ()<UITableViewDataSource,UITableViewDelegate,SDCycleScrollViewDelegate>{
    UITableView *tableV;
    NSDictionary *dic;
    int a;
}


@end

@implementation FangChanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    a=4;
    NSString *httpArg =[NSString stringWithFormat:@"num=10&page=%d",a];
    NSString *urlStr = [[NSString alloc]initWithFormat: @"%@?%@", TEST_URL, httpArg];
    NSURL *url = [NSURL URLWithString: urlStr];
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc]initWithURL:url];
    [request addRequestHeader:@"apikey" value:@"44149ec2b7a1fc46f8e9102a4cd8061a"];
    ASIHTTPRequest*newRequst =request;
    [request setCompletionBlock:^{
        NSData *data = newRequst.responseData;
        dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        [tableV reloadData];

    }];
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    //设置缓存方式
    [request setDownloadCache:appDelegate.myCache];
    //设置缓存数据存储策略，这里采取的是如果无更新或无法联网就读取缓存数据
    [request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
    request.delegate = self;
    [request startAsynchronous];
    self.automaticallyAdjustsScrollViewInsets=YES;
    //初始化表格
    tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    tableV.delegate=self;
    tableV.dataSource=self;
        tableV.mj_header=[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(header)];
    [self.view addSubview:tableV];
}
-(void)header{
    a++;
    NSString *httpArg =[NSString stringWithFormat:@"num=10&page=%d",a];
    NSString *urlStr = [[NSString alloc]initWithFormat: @"%@?%@", TEST_URL, httpArg];
    NSURL *url = [NSURL URLWithString: urlStr];
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc]initWithURL:url];
    [request addRequestHeader:@"apikey" value:@"44149ec2b7a1fc46f8e9102a4cd8061a"];
    ASIHTTPRequest*newRequst =request;
    [request setCompletionBlock:^{
        NSData *data = newRequst.responseData;
        dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    }];
    [request startSynchronous];
    [tableV.mj_header endRefreshing];
    [tableV reloadData];
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    NSArray *aa  = [dic objectForKey:@"newslist"];
    NSMutableArray *bb=[NSMutableArray array];
    for (NSDictionary *dd in aa) {
        [bb addObject:[dd objectForKey:@"picUrl"]];
    }
    NSArray *imageArray=@[@"http://imgsrc.baidu.com/baike/pic/item/a0ca99d68f1ac76307088b97.jpg",@"http://epaper.xiancn.com/xarb/res/1/20100227/58731267215555820.jpg",@"http://www.xwzgw.com/pic/news/201512/14497349184_0.jpg",@"http://www.kf.cn/kfrb/rmp/1/1/2014-06/13/08/res01_attpic_brief.jpg"];
    SDCycleScrollView *cycleView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) delegate:self placeholderImage:[UIImage imageNamed:@"home_header_slogon@2x.png"]];
    cycleView.imageURLStringsGroup =imageArray;
    return cycleView;
}
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section{
    return 200;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *dd  = [dic objectForKey:@"newslist"];
    return dd.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString*ss=@"love";
    NewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ss];
    if(!cell){
        cell= [[NewTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ss];
    }
    NSArray *aa  = [dic objectForKey:@"newslist"];
    NSDictionary *dic_2 =aa[indexPath.row];
    NSURL *url = [NSURL URLWithString:[dic_2 objectForKey:@"picUrl"]];
    NSData *data = [NSData dataWithContentsOfURL:url];
    cell.imageV.image = [UIImage imageWithData:data scale:20];
    cell.imageV.image = [UIImage imageWithData:data];
    cell.fuLab.text= [dic_2 objectForKey:@"title"];
    cell.leftLab.text=[dic_2 objectForKey:@"ctime"];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *aa  = [dic objectForKey:@"newslist"];
    NSDictionary *dic_2 =aa[indexPath.row];
    NSString *string =[dic_2 objectForKey:@"url"];
    CATransition *tran = [CATransition animation];
    tran.duration=1.5;
    NextFangChanViewController *ss=[[NextFangChanViewController alloc]init];
    ss.ss=string;
    //    tran.type=@"cameraIrisHollowOpen";
    //    pageCurl            向上翻一页
    //    pageUnCurl          向下翻一页
    //    rippleEffect        滴水效果
    //    suckEffect          收缩效果，如一块布被抽走
    //    cube                立方体效果
    //    oglFlip             上下翻转效果
    tran.type=@"rippleEffect";
    tran.subtype=kCAAlignmentLeft;
    [self.view.window.layer addAnimation:tran forKey:nil];
    [self presentViewController:ss animated:YES completion:nil];
    
    //    }else if (indexPath.row==1){
    //        NextTouTiaoViewController *tt= [[NextTouTiaoViewController alloc]init];
    //        NSString *string =@"ttp://c.m.163.com/news/a/C0I44QUL00051C89.html?spss=newsapp&spsw=1";
    //        tt.ss=string;
    //        CATransition *tran = [CATransition animation];
    //        tran.duration=3;
    //        //    tran.type=@"cameraIrisHollowOpen";
    //        //    pageCurl            向上翻一页
    //        //    pageUnCurl          向下翻一页
    //        //    rippleEffect        滴水效果
    //        //    suckEffect          收缩效果，如一块布被抽走
    //        //    cube                立方体效果
    //        //    oglFlip             上下翻转效果
    //        tran.type=@"rippleEffect";
    //        tran.subtype=kCAAlignmentLeft;
    //
    //        [self presentViewController:tt animated:YES completion:nil];
    //
    //    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
